// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    athletes: '',
    userInfo: {
    avatarUrl: defaultAvatarUrl,
    nickName: '',
    indicatorDots:true,
    circular:true,
    autoplay:true,
    newsList: '',
    ip: '',
    imgsrc: '',
    images: ['index1.png', 'index2.png', 'index3.png', 'index4.png', 'index5.png']
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var app = getApp()
    var ip = app.globalData.ServerAddress
    let that = this
    this.setData({
      ip: ip
    })
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: "http://" + ip + "/athlete/all",
      method: 'GET',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        
        "authorization": app.globalData.token
      },
      data:{
        is_search: 'false',
        page: 1,
        per_page: 10,
        search_info: 'none'
      },
      success:function (res){
        console.log(res.data.athletes)
        let list = res.data.athletes
        // console.log(list)
        list.sort(() => Math.random() - 0.5);
        console.log(list)
        that.setData({
          athletes: list,
        })
      }
    }),
    wx.request({
      url: "http://" + ip + "/news/all",
      method: 'GET',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data:{
        
        
      },
      success:function (res){
        console.log(res.data)
        that.setData({
          newsList: res.data.posts.slice(0,3)
        })
       console.log(that.data.newsList)
      },
      complete: () => {
        //隐藏loading效果
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
